package com.example.mall.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mall.dto.ProductImagesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IProductImagesService;
import com.example.mall.domain.ProductImages;
import com.example.mall.query.ProductImagesQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.result.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/productImages")
public class ProductImagesController {

    @Autowired
    public IProductImagesService productImagesService;

    @GetMapping("selectByProductId/{productId}")
    @ApiOperation(value = "根据productId查询商品图片表信息",notes = "根据productId查询商品图片表信息",httpMethod = "GET")
    public JSONResult selectByProductId(@PathVariable("productId")Long productId){
        List<ProductImages> productImages = productImagesService.selectByProductId(productId);
        return JSONResult.success("查询成功",productImages);
    }
    /**
     * 分页查询商品图片表信息
     * @param productImagesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品图片表信息",notes = "分页查询商品图片表信息",httpMethod = "GET")
    public JSONResult queryPage(ProductImagesQuery productImagesQuery){
        PageDTO<ProductImagesDto> productImagesDtoPageDTO = productImagesService.queryPage(productImagesQuery);
        return JSONResult.success("查询成功",productImagesDtoPageDTO);
    }

    /**
     * 根据id查询商品图片表信息
     * @param imageId
     * @return
     */
    @GetMapping("query/{imageId}")
    @ApiOperation(value = "根据id查询商品图片表信息",notes = "根据id查询商品图片表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("imageId")Long imageId){
        //1.根据id查询用户信息
        if (imageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.IMAGEID_NOT_ID);
        }
        ProductImages productImages = productImagesService.getById(imageId);
        return JSONResult.success("查询成功",productImages);
    }

    /**
     * 根据id查询商品图片表信息
     * @param imageId
     * @return
     */
    @GetMapping("queryProductImagesById/{imageId}")
    @ApiOperation(value = "根据id查询商品图片表信息",notes = "根据id查询商品图片表信息",httpMethod = "GET")
    public JSONResult queryProductImagesById(@PathVariable("imageId")Long imageId){
        //1.根据id查询用户信息
        if (imageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.IMAGEID_NOT_ID);
        }
        ProductImages productImages = productImagesService.queryProductImagesById(imageId);
        return JSONResult.success("查询成功",productImages);
    }

    /**
     * 查询全部商品图片表信息
     * @param
     * @return
     */
    @GetMapping("queryProductImagesList")
    @ApiOperation(value = "查询全部商品图片表信息",notes = "查询全部商品图片表信息",httpMethod = "GET")
    public JSONResult queryProductImagesList(){
        List<ProductImages> productImages = productImagesService.queryProductImagesList();
        return JSONResult.success("查询成功",productImages);
    }

    /**
     * 新增商品图片表
     * @param productImagesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品图片表",notes = "新增商品图片表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ProductImagesDto productImagesDto){
        productImagesService.insert(productImagesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品图片表信息
     * @param productImagesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品图片表信息",notes = "修改商品图片表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ProductImagesDto productImagesDto){
        productImagesService.update(productImagesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品图片表信息
     * @param imageId
     * @return
     */
    @PostMapping("delete/{imageId}")
    @ApiOperation(value = "删除商品图片表信息",notes = "删除商品图片表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("imageId") Long imageId) {
        productImagesService.delete(imageId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品图片表信息
     * @param imageIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品图片表信息",notes = "批量删除商品图片表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> imageIds) {
        productImagesService.deleteBatch(imageIds);
        return JSONResult.success("批量删除成功");
    }
}
